package com.xiaobai.thea;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class AuthUtil {
    public static String login(String user, String password)  {
        var request = HttpRequest.newBuilder().uri(URI.create("https://chat.fengzhengx.cn/api/apiKey"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString("{\"email\":\"" + user + "\",\"password\":\"" + password +  "\",\"captcha\":\"\"}")).build();
        final HttpClient client = HttpClient.newHttpClient();
        String response;
        try {
            response = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
            response = response.substring(response.indexOf("\"data\":") + 7);
            response = response.replace("}", "");
            response = response.replace("\"", "");
            response = response.replace("null", "");
            return response;
        } catch (IOException | InterruptedException e) {
            return null;
        }
    }
}
